<?php
	
class StatisticType
{
	const WEB_STAT_LOAD_MAIN_SITE = 0;
	const WEB_STAT_LOGIN_USER = 1;
	const WEB_STAT_RESERVATION_DONE = 2;
	const WEB_STAT_RESERVATION_CANCELED = 3;
	const WEB_STAT_USER_REGISTERED = 4;
	const WEB_STAT_FORGOTTEN_PASSWORD = 5;
	const WEB_STAT_PASSWORD_CHANGED = 6;
}

class WebStat
{
    static public function logStatistic( $actionType, $actionData )
    {    
		$ci = & get_instance();
		$ci->load->model('webstat_model');
		return $ci->model->logStatistic($actionType, strToHex( $actionData ));
    }
    
    static private function prettyDate( $date )
    {
        $ar = explode( "-", $date );
        for ($i = 1; $i < 3; $i ++)
        {
            $ar[i] = $ar[i] < 10 ? sprintf( "0%d", $ar[i] ) : sprintf( "%d", $ar[i] );
        }
        
        return sprintf( "%d.%d.%d", $ar[2], $ar[1], $ar[0] );
    }
    
    static public function logLoadMainSite()
    {
        $dbStr = strToHex( getRealIpAddr() );
        WebStat::logStatistic( StatisticType::WEB_STAT_LOAD_MAIN_SITE, $dbStr );
    }

    static public function logReservationDone( $emailAddress, $reservationDate, $courtNumber, $reservationTime )
    {
        $dbStr = strToHex( $emailAddress ) . "|" . strToHex( WebStat::prettyDate( $reservationDate ) ) . "|" . strToHex( $courtNumber ) . "|" . strToHex( $reservationTime );
        WebStat::logStatistic( StatisticType::WEB_STAT_RESERVATION_DONE, $dbStr );
    }
    
    static public function logReservationCanceled( $emailAddress, $reservationDate, $courtNumber, $reservationTime )
    {
        $dbStr = strToHex( $emailAddress ) . "|" . strToHex( WebStat::prettyDate( $reservationDate ) ) . "|" . strToHex( $courtNumber ) . "|" . strToHex( $reservationTime );
        WebStat::logStatistic( StatisticType::WEB_STAT_RESERVATION_CANCELED, $dbStr );
    }
    
    static public function logUserRegistered( $emailAddress )
    {
        $dbStr = strToHex( $emailAddress );
        WebStat::logStatistic( StatisticType::WEB_STAT_USER_REGISTERED, $dbStr );
    }
    
    static public function logForgottenPassword( $emailAddress )
    {
        $dbStr = strToHex( $emailAddress );
        WebStat::logStatistic( StatisticType::WEB_STAT_FORGOTTEN_PASSWORD, $dbStr );
    }
    
    static public function logPasswordChanged( $emailAddress )
    {
        $dbStr = strToHex( $emailAddress );
        WebStat::logStatistic( StatisticType::WEB_STAT_PASSWORD_CHANGED, $dbStr );
    }
}

?>
